package au.com.handsunited.jodawrapper;

import java.io.Serializable;
import java.util.Date;
import java.util.Locale;

import javax.persistence.Embeddable;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author arunc
 */
@Embeddable
public class JodaDateTimeWrapper implements Comparable<JodaDateTimeWrapper>,
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1433288884983359583L;

	private DateTime date;

	private static Logger logger = LoggerFactory
			.getLogger(JodaDateTimeWrapper.class);

	/**
	 *
	 */
	public JodaDateTimeWrapper() {
		super();
		date = new DateTime();
	}

	/**
	 * 
	 * @param day
	 * @param month
	 * @param year
	 */
	public JodaDateTimeWrapper(int day, int month, int year) {
		this();
		setDay(day);
		setMonth(month);
		setYear(year);
	}

	/**
	 * @param date
	 */
	public JodaDateTimeWrapper(DateTime date) {
		this();
		this.date = new DateTime(date);
	}

	
	/**
	 * 
	 * @param timeInMillis
	 */
	public JodaDateTimeWrapper(long timeInMillis) {
		this();
		this.date = new DateTime(timeInMillis);
	}
	
	/**
	 * 
	 * @param date
	 */
	public JodaDateTimeWrapper(Date date) {
		this(date.getTime());
	}
	
	
	
	
	/**
	 * @return the date
	 */
	public DateTime getDate() {
		return date;
	}

	/**
	 * @param date
	 *            the date to set
	 */
	public void setDate(DateTime date) {
		this.date = date;
	}

	/**
	 * 
	 * @param year
	 */
	public void setYear(int year) {
		if (date == null) {
			date = new DateTime();
		}
		date = date.withYear(year);
	}

	/**
	 * 
	 * @param year
	 */
	public void setYearStr(String year) {
		if (StringUtils.isNotBlank(year)) {
			setYear(Integer.parseInt(year));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getYear() {
		if (date == null) {
			return 0;
		} else {
			return date.getYear();
		}
	}

	/**
	 * 
	 * @return
	 */
	public String getYearStr() {
		return StringUtils.leftPad(getYear() + "", 4, '0');
	}

	/**
	 * 
	 * @param month
	 */
	public void setMonth(int month) {
		if (date == null) {
			date = new DateTime();
		}
		date = date.withMonthOfYear(month);
	}

	/**
	 * 
	 * @param month
	 */
	public void setMonthStr(String month) {
		if (StringUtils.isNotBlank(month)) {
			setMonth(Integer.parseInt(month));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getMonth() {
		if (date == null) {
			return 0;
		} else {
			return date.getMonthOfYear();
		}
	}

	/**
	 * 
	 * @return
	 */
	public String getMonthStr() {
		return StringUtils.leftPad(getMonth() + "", 2, '0');
	}

	/**
	 * 
	 * @param day
	 */
	public void setDay(int day) {
		if (date == null) {
			date = new DateTime();
		}
		date = date.withDayOfMonth(day);
	}

	/**
	 * 
	 * @param day
	 */
	public void setDayStr(String day) {
		if (StringUtils.isNotBlank(day)) {
			setDay(Integer.parseInt(day));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getDay() {
		if (date == null) {
			return 0;
		} else {
			return date.getDayOfMonth();
		}
	}

	/**
	 * 
	 * @return
	 */
	public String getDayStr() {
		return StringUtils.leftPad(getDay() + "", 2, '0');
	}

	/**
	 * 
	 * @param hour
	 */
	public void setHour(int hour) {
		if (date == null) {
			date = new DateTime();
		}
		date = date.withHourOfDay(hour);
	}

	/**
	 * 
	 * @param hour
	 */
	public void setHourStr(String hour) {
		if (StringUtils.isNotBlank(hour)) {
			setHour(Integer.parseInt(hour));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getHour() {
		if (date == null) {
			return 0;
		} else {
			return date.getHourOfDay();
		}
	}

	/**
	 * 
	 * @return
	 */
	public String getHourStr() {
		return StringUtils.leftPad(getHour() + "", 2, '0');
	}

	/**
	 * 
	 * @param minute
	 */
	public void setMinute(int minute) {
		if (date == null) {
			date = new DateTime();
		}
		date = date.withMinuteOfHour(minute);
	}

	/**
	 * 
	 * @param minute
	 */
	public void setMinuteStr(String minute) {
		if (StringUtils.isNotBlank(minute)) {
			setMinute(Integer.parseInt(minute));
		}
	}

	/**
	 * 
	 * @return
	 */
	public int getMinute() {
		if (date == null) {
			return 0;
		} else {
			return date.getMinuteOfHour();
		}
	}

	/**
	 * 
	 * @return
	 */
	public String getMinuteStr() {
		return StringUtils.leftPad(getMinute() + "", 2, '0');
	}

	/**
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(JodaDateTimeWrapper jdtw) {
		return date.compareTo(jdtw.getDate());
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return date.toString();
	}

	/**
	 * 
	 * @param formatter
	 * @return
	 */
	public String toString(DateTimeFormatter formatter) {
		return date.toString(formatter);
	}

	/**
	 * 
	 * @param pattern
	 * @return
	 */
	public String toString(String pattern) {
		return date.toString(pattern);
	}

	/**
	 * 
	 * @param pattern
	 * @param locale
	 * @return
	 */
	public String toString(String pattern, Locale locale) {
		return date.toString(pattern, locale);
	}

}
